package com.ronghui.cash.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ronghui.cash.entity.RuleLoan;
import com.ronghui.cash.parambean.CalcFenqiMoenyBean;
import com.ronghui.cash.service.RuleLoanService;
import com.ronghui.cash.util.CalcRateUtil;
import com.ronghui.cash.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class WebController extends BaseController {
	// private static Logger log = Logger.getLogger(WebController.class);

	@Autowired
	RuleLoanService ruleLoanService;

	/**
	 * 系统设定的借款规则
	 * @return
	 */
	@RequestMapping("/ruleLoan")
	public Response calcFenqiMoney(){
		//查询系统设定的借款规则
		RuleLoan ruleLoan = ruleLoanService.selectOne(new EntityWrapper<RuleLoan>());
		String[] canBorrowMoneys = ruleLoan.getMoney().split(",");//可借金额

		List<Map<String,Object>> canBorrowMoneysList = new ArrayList<>();
		Map<String,Object> item = null;
		for(String canBorrowMoney:canBorrowMoneys) {
			item = new HashMap<>();
			item.put("canBorrowMoney",canBorrowMoney);
			canBorrowMoneysList.add(item);
		}
		Map<String,Object> map = new HashMap<>();
		map.put("canBorrowMoneys",canBorrowMoneysList);//可借款金额列表
		map.put("serviceStatus",ruleLoan.getServiceStatus());//借款服务状态：1：上线借款服务，0：关闭借款服务
		map.put("manyTimesStatus",ruleLoan.getManyTimesStatus());//多次借款:0:再次借款时，需要先还清上一笔借款,1:再次借款时，不需要先还清上一笔借款
		map.put("dayRate",ruleLoan.getDayRate());//日利率,单位为‰
		map.put("overdueRate",ruleLoan.getOverdueRate());//逾期利率：单位为‰
		return success(map);
	}

	/**
	 * 计算分期金额
	 * 
	 * @param cb
	 * @return
	 */
	@RequestMapping("/calcFenqiMoney")
	public Response calcFenqiMoney(@Validated CalcFenqiMoenyBean cb) {
		Double money = cb.getMoney();

		//查询系统设定的借款规则
		RuleLoan ruleLoan = ruleLoanService.selectOne(new EntityWrapper<RuleLoan>());

		Double dayRate = ruleLoan.getDayRate().doubleValue();//日利率
		String[] canBorrowMoneys = ruleLoan.getMoney().split(",");//可借金额
		String[] timeLimit = ruleLoan.getTimeLimit().split(",");//可借天数

		Integer day = 0;
		List<Map<String,Object>> list = null;
		Map<String,Object> item = null;
		for(String canBorrowMoney:canBorrowMoneys){
			if(money==Double.parseDouble(canBorrowMoney)){
				list = new ArrayList<>();
				for(int i=0;i<timeLimit.length;i++){
					item = new HashMap<>();
					day = Integer.valueOf(timeLimit[i]);
					CalcRateUtil cr = new CalcRateUtil(dayRate, money, i+1,0d, day);
					item.put("day", day); //借款天数
					item.put("interest",cr.getInterest()); //借款总利息（手续费）
					item.put("actualMoney",cr.getActualMoney());//实际到账金额
					list.add(item);
				}
				//根据期数排序
				Collections.sort(list, new Comparator<Map<String, Object>>() {
					public int compare(Map<String, Object> o1, Map<String, Object> o2) {
						Integer name1 = Integer.valueOf(o1.get("day").toString()) ;//name1是从你list里面拿出来的一个
						Integer name2 = Integer.valueOf(o2.get("day").toString()) ; //name1是从你list里面拿出来的第二个name
						return name1.compareTo(name2);
					}
				});
			}
		}
		if(list==null){
			return failure("借款金额异常");
		}else{
			return success(list);
		}
	}
}
